package com.zyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyb.mapper.StudentMapper;
import com.zyb.pojo.LoginForm;
import com.zyb.pojo.Student;
import com.zyb.service.StudentService;
import com.zyb.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    //登录校验
    @Override
    public Student login(LoginForm loginForm) {
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        wrapper.eq("name",loginForm.getUsername());
        wrapper.eq("password", MD5.encrypt(loginForm.getPassword()));
        Student student = baseMapper.selectOne(wrapper);
        return student;
    }

    //获取登录人的信息并查询
    @Override
    public Student getAdminById(Long userId) {
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        wrapper.eq("id",userId);
        return baseMapper.selectOne(wrapper);
    }

    //条件查询带分页
    @Override
    public IPage<Student> getStudentPage(Integer pageNo, Integer pageSize, Student student) {
        IPage<Student> studentIPage=new Page<>(pageNo,pageSize);
        String name = student.getName();
        String clazzName = student.getClazzName();
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(clazzName)){
            wrapper.like("clazz_name",clazzName);
        }
        wrapper.orderByDesc("id");
        IPage<Student> iPage = baseMapper.selectPage(studentIPage, wrapper);
        return iPage;
    }
}
